#! /bin/sh

name=bzip2
repology_srcname=bzip2
version=1.0.8
revision=1
tarball_url="https://sourceware.org/ftp/bzip2/bzip2-${version}.tar.gz"
tarball_blake2b="22ab3acd84f4db8c3d6f59340c252faedfd4447cea00dafbd652e65b6cf8a20adf6835c22e58563004cfafdb15348c924996230b4b23cae42da5e25eeac4bdad"
hostdeps="gcc pkg-config"
deps="core-libs"

build() {
    cp -rp ${source_dir}/. .

    make -f Makefile-libbz2_so CC="${OS_TRIPLET}-gcc $TARGET_CFLAGS $TARGET_CPPFLAGS $TARGET_LDFLAGS"
    make bzip2 bzip2recover CC="${OS_TRIPLET}-gcc $TARGET_CFLAGS $TARGET_CPPFLAGS $TARGET_LDFLAGS" AR="${OS_TRIPLET}-ar"
}

package() {
    install -dm755 "${dest_dir}${prefix}/"{bin,lib,lib/pkgconfig,include,share/man/man1}

    install -m755 bzip2-shared "${dest_dir}${prefix}/bin/bzip2"
    install -m755 bzip2recover bzdiff bzgrep bzmore "${dest_dir}${prefix}/bin/"
    ln -sf bzip2 "${dest_dir}${prefix}/bin/bunzip2"
    ln -sf bzip2 "${dest_dir}${prefix}/bin/bzcat"

    cp -dR libbz2.so* "${dest_dir}${prefix}/lib/"
    ln -s libbz2.so.${version} "${dest_dir}${prefix}/lib/libbz2.so"
    ln -s libbz2.so.${version} "${dest_dir}${prefix}/lib/libbz2.so.1"

    install -m644 bzlib.h "${dest_dir}${prefix}/include/"

    install -m644 bzip2.1 "${dest_dir}${prefix}/share/man/man1/"
    ln -sf bzip2.1 "${dest_dir}${prefix}/share/man/man1/bunzip2.1"
    ln -sf bzip2.1 "${dest_dir}${prefix}/share/man/man1/bzcat.1"
    ln -sf bzip2.1 "${dest_dir}${prefix}/share/man/man1/bzip2recover.1"

    cat <<EOF >"${dest_dir}${prefix}/lib/pkgconfig/bzip2.pc"
prefix=${prefix}
exec_prefix=\${prefix}
bindir=\${exec_prefix}/bin
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: bzip2
Description: A file compression library
Version: ${version}
Libs: -L\${libdir} -lbz2
Cflags: -I\${includedir}
EOF

    post_package_strip
}
